IF EXISTS (SELECT * FROM sysobjects WHERE name = 'GetInstanceNameFromMSSQLFullInstanceName' AND type IN ('FN','IF','TF'))
     DROP FUNCTION dbo.GetInstanceNameFromMSSQLFullInstanceName
GO

CREATE FUNCTION dbo.GetInstanceNameFromMSSQLFullInstanceName
     ( @FullInstanceName varchar(255) )
RETURNS varchar(128)
AS
/*
 *
 * GetInstanceNameFromMSSQLFullInstanceName
 *
 * Purpose:      
 *
 * Inputs:       
 *
 * Assumes:      
 *
 * Effects:      
 *
 * Returns:      
 *
 * Calling
 * Application:  
 *
 * Note(s):      
 *
 * CfgCtrl Keys: 
 *
 * Author(s):    NOVA Concepts Ltd.
 *
 * Change Log:   Initial Release, 10/19/2011, 1.00.0000
 *
 */
BEGIN
     DECLARE @ReturnValue varchar(128)

     SET @FullInstanceName = LTRIM( RTRIM( @FullInstanceName ) )

     IF ( CHARINDEX( '\', @FullInstanceName ) > 0 )
     BEGIN -- We have a \ which indicates named instance
          SELECT @ReturnValue = RIGHT( @FullInstanceName, LEN( @FullInstanceName ) - CHARINDEX( '\', @FullInstanceName ) )
     END
     ELSE
     BEGIN -- We do not have a \ indicating default instance
          SELECT @ReturnValue = ''
     END

     RETURN @ReturnValue
END
GO
